package com.haochang.chunkvod.view.vod.spinner;

import java.util.ArrayList;

import com.haochang.base.BaseTextView;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 菜单控件头部，封装了下拉动画，动态生成头部按钮个数
 * 
 * @author qintao
 */

public class ExpandTabView extends LinearLayout implements OnDismissListener
{

	private RelativeLayout selectedButton;
	private ArrayList<String> mTitleArray = new ArrayList<String>();
	private ArrayList<RelativeLayout> mViewArray = new ArrayList<RelativeLayout>();
	private ArrayList<BaseTextView> mItemText = new ArrayList<BaseTextView>();
	private Context mContext;
	private PopupWindow popupWindow;
	private int displayWidth;
	private int displayHeight;
	private int selectPosition;

	public ExpandTabView(Context context)
	{
		super(context);
		init(context);
	}

	public ExpandTabView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init(context);
	}

	/**
	 * 根据选择的位置设置tabitem显示的值
	 */
	public void setTitle(String valueText, int position)
	{
		if (position < mItemText.size())
		{
			mItemText.get(position).setText(valueText);
		}
	}

	/**
	 * 根据选择的位置获取tabitem显示的值
	 */
	public String getTitle(int position)
	{
		if (position < mItemText.size() && mItemText.get(position).getText() != null)
		{
			return mItemText.get(position).getText().toString();
		}
		return "";
	}

	/**
	 * 初始化头部title
	 * @param textArray
	 */
	public void initView(ArrayList<String> textArray)
	{
		if (mContext == null)
			return;
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mTitleArray.addAll(textArray);
		for (int i = 0; i < textArray.size(); i++)
		{
			RelativeLayout tButton = (RelativeLayout) inflater.inflate(R.layout.view_spinner_layout, this, false);
			BaseTextView tv_title = (BaseTextView) tButton.findViewById(R.id.tv_title);
			addView(tButton);
			View line = new TextView(mContext);
			line.setBackgroundColor(mContext.getResources().getColor(R.color.color_e3e6e6));
			if (i < textArray.size() - 1)
			{
				LayoutParams lp = new LayoutParams(DipPxConversion.dip2px(mContext, 1), DipPxConversion.dip2px(mContext, 28));
				addView(line, lp);
			}
			mItemText.add(tv_title);
			tv_title.setText(mTitleArray.get(i));
			tButton.setTag(i);
			tButton.setOnClickListener(new OnClickListener()
			{

				@Override
				public void onClick(View view)
				{
					boolean isCleanCurrentBtn = false;
					RelativeLayout tButton = (RelativeLayout) view;
					if (selectedButton == null)
					{
						selectedButton = tButton;
						selectedButton.setSelected(true);
					} else if (selectedButton != tButton)
					{
						selectedButton.setSelected(false);
						selectedButton = tButton;
						selectedButton.setSelected(true);
					} else
					{
						if (!selectedButton.isSelected())
						{
							selectedButton.setSelected(true);
						} else
						{
							selectedButton.setSelected(false);
							isCleanCurrentBtn = true;
						}
					}
					selectPosition = (Integer) selectedButton.getTag();
					startAnimation(isCleanCurrentBtn);
				}
			});
		}
	}

	/**
	 * 设置tabitem的个数和初始值
	 */
	public void setValue(ArrayList<View> viewArray)
	{
		if (mContext == null)
			return;
		mViewArray.clear();
		for (int i = 0; i < viewArray.size(); i++)
		{
			RelativeLayout contentView = (RelativeLayout) viewArray.get(i);
			mViewArray.add(contentView);
			contentView.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					onPressBack();
				}
			});
			contentView.setBackgroundColor(mContext.getResources().getColor(R.color.black));
			contentView.getBackground().setAlpha(120);
		}
	}

	private void startAnimation(boolean isCleanCurrentBtn)
	{
		if (CollectionUtils.isEmpty(mViewArray))
			return;
		if (popupWindow == null)
		{
			popupWindow = new PopupWindow(mViewArray.get(selectPosition), displayWidth, displayHeight);
			popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
			popupWindow.setFocusable(false);
			popupWindow.setOutsideTouchable(true);
		}
		if (selectedButton.isSelected())
		{
			if (!popupWindow.isShowing())
			{
				showPopup(selectPosition);
			} else
			{
				popupWindow.setOnDismissListener(this);
				popupWindow.dismiss();
			}
		} else
		{
			if (popupWindow.isShowing())
			{
				popupWindow.dismiss();
			}
		}
		if (isCleanCurrentBtn)
			selectedButton = null;
	}

	private void showPopup(int position)
	{
		if (popupWindow.getContentView() != mViewArray.get(position))
		{
			popupWindow.setContentView(mViewArray.get(position));
		}
		popupWindow.showAsDropDown(this, 0, 0);
	}

	/**
	 * 如果菜单成展开状态，则让菜单收回去
	 */
	public boolean onPressBack()
	{
		if (popupWindow != null && popupWindow.isShowing())
		{
			popupWindow.dismiss();
			if (selectedButton != null)
			{
				selectedButton.setSelected(false);
			}
			return true;
		} else
		{
			return false;
		}
	}

	private void init(Context context)
	{
		if (this.isInEditMode())
		{
			return;
		}
		mContext = context;
		displayWidth = ((Activity) mContext).getWindowManager().getDefaultDisplay().getWidth();
		displayHeight = ((Activity) mContext).getWindowManager().getDefaultDisplay().getHeight();
		setOrientation(LinearLayout.HORIZONTAL);
	}

	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
	}

	@Override
	public void onDismiss()
	{
		showPopup(selectPosition);
		popupWindow.setOnDismissListener(null);
	}

}
